﻿using Wen.Core.Extensions;
using WenAntdUI.Extensions;
using WenAntdUI.Helps;
using WenAntdUI.Services;

namespace WenAntdUI.Pages
{
    [Description("用户信息设置")]
    public partial class UserInfoPage : UserControl
    {
        IFreeSql fsql;
        IdentityService identity;
        UserService userService;
        public UserInfoPage(IFreeSql fsql, IdentityService identity, UserService userService)
        {
            InitializeComponent();
            this.fsql = fsql;
            this.identity = identity;
            this.userService = userService;

            avatar1.BackColor = Color.Green;
            avatar1.Font = new Font("黑体", 50, FontStyle.Bold);
            avatar1.ForeColor = Color.White;
            avatar1.Round = true;
            avatar1.Text = "无";
            Initcom();
        }

        private void Initcom()
        {
            var user = identity.SysUser;

            try
            {
                var image = ImageHelp.GetImage(identity.SysUser.Icon);
                avatar1.Image = image;
            }
            catch { }
        }

        private void Avatar1_Click(object sender, EventArgs e)
        {
            var dia = new OpenFileDialog()
            {
                Filter = "*.png|*.png"
            };
            if (dia.ShowDialog() == DialogResult.OK)
            {
                var image = new Bitmap(dia.FileName);
                avatar1.Image = image;
            }
        }

        private void Button2_Click(object sender, EventArgs e)
        {
            var iconString = ImageHelp.ToBase64String(avatar1.Image);
            fsql.Update<SysUser>().Set(a => a.Icon == iconString).Where(identity.SysUser).ExecuteEffects();
            Msgbox.ShowSuccess("保存成功");
        }

        private void Button1_Click(object sender, EventArgs e)
        {
            if (!userService.VerifyLogin(identity.UserName, input1.Text))
            {
                Msgbox.ShowError("原密码不正确");
                return;
            }

            userService.ResetPassword(identity.UserName, input2.Text);
            Msgbox.ShowSuccess("密码修改成功");
        }
    }
}
